package com.hk.common.utils;

import com.hk.common.constant.Constants;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {


    /** 时间格式(HH:mm:ss) */
    public final static String TIME_PATTERN = "HH:mm:ss";

    /** 时间格式(yyyy-MM-dd) */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /** 时间格式(yyyy-MM-dd HH:mm:ss) */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public final static String CDA_PARAM_DATE_PATTERN = "yyyyMMdd";

    public final static String DATE_SECOND_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";


    public static Date getDateNoTime(Date date) {
        try{
            date = parse(format(date));
        }catch (Exception e){
            e.printStackTrace();
        }
        return date;
    }

    public static Date parse(String date) {
        return parse(date, DATE_PATTERN);
    }

    public static Date parse(String date,String pattern){
        if(date != null){
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            try {
                return df.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     * @param date  日期
     * @return  返回yyyy-MM-dd格式日期
     */
    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    /**
     * 日期格式化 日期格式为：HH:mm:ss
     * @param date
     * @return
     */
    public static String formatTime(Date date) {
        return format(date, TIME_PATTERN).substring(0,5);
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     * @param date  日期
     * @param pattern  格式，如：DateUtils.DATE_TIME_PATTERN
     * @return  返回yyyy-MM-dd格式日期
     */
    public static String format(Date date, String pattern) {
        if(date != null){
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return "";
    }

    /**
     * 字符串转换成日期
     * @param strDate 日期字符串
     * @param pattern 日期的格式，如：DateUtils.DATE_TIME_PATTERN
     */
    public static Date stringToDate(String strDate, String pattern) {
        if (StringUtils.isEmpty(strDate)){
            return null;
        }
        SimpleDateFormat fmt = new SimpleDateFormat(pattern);

        try {
            return fmt.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当天日期的时间数据，去除时分秒
     *
     */
    public static String curDateStr(){
        return format(new Date(),DATE_PATTERN);
    }

    /**
     * 获取当天日期的时间数据，包含时分秒
     *
     */
    public static String curDateTimeStr(){
        return format(new Date(),DATE_TIME_PATTERN);
    }

    /**
     *添加天数，返回日期格式
     */
    public static String addDayStr(Date date, int num){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH,num);
        return format(cal.getTime(),DATE_PATTERN);
    }

    /**
     *添加天数，返回日期格式
     */
    public static Date addDay(Date date,int num){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH,num);
        return cal.getTime();
    }

    /**
     * 获取当天午别情况
     *
     */
    public static String currentNoon(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if(hour<= Constants.NOON_HOUR){
            return Constants.NOON_AM;
        }else{
            return Constants.NOON_PM;
        }
    }

    /**
     * 获取上周开始时间  以周一为开始周日结束
     * @param date
     * @return
     */
    public static String lastWeekStartDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_MONTH,0-6-dayWeek+1);
        return format(cal.getTime(),DATE_PATTERN);
    }

    /**
     * 获取上周结束时间  以周一为开始周日结束
     * @param date
     * @return
     */
    public static String lastWeekEndDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_MONTH,0-dayWeek+1);
        return format(cal.getTime(),DATE_PATTERN);
    }

    /**
     * 获取当前时间的小时数
     * @param date
     * @return
     */
    public static int getCurrentHour(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.HOUR_OF_DAY);
    }


    /**
     * 根据日期字符串 2020-01-02 获取当月开始时间
     * @param date
     * @return
     */
    public static Date curMonthStartDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH,1);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        return cal.getTime();
    }

    /**
     * 根据日期字符串 2020-01-02 获取当月结束时间
     * @param date
     * @return
     */
    public static Date curMonthEndDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH,1);
        cal.add(Calendar.MONTH,1);
        cal.add(Calendar.DAY_OF_MONTH,-1);
        cal.set(Calendar.HOUR_OF_DAY,23);
        cal.set(Calendar.MINUTE,59);
        cal.set(Calendar.SECOND,59);
        return cal.getTime();
    }

    public static Date add(Date date,int field, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(field,amount);
        return cal.getTime();
    }

    public static String getCdaParamEndDate(){
        Date date = new Date();
        return format(date,CDA_PARAM_DATE_PATTERN);
    }

    /**
     * 获取周日 （按周日为结束算）
     * @param date
     * @return
     */
    public static Date getSunday(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 直接设置周日会是本周日（星期以周日开始 需要+7）
        if(cal.get(Calendar.DAY_OF_WEEK)!=Calendar.SUNDAY){
            cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
            cal.add(Calendar.DAY_OF_MONTH,7);
        }
        return getDateNoTime(cal.getTime());
    }


    /**
     * 获取当天结束时间
     * @param date
     * @return
     */
    public static Date getEndTime(Date date) {
        Calendar dateEnd = Calendar.getInstance();
        dateEnd.setTime(date);
        dateEnd.set(Calendar.HOUR_OF_DAY, 23);
        dateEnd.set(Calendar.MINUTE, 59);
        dateEnd.set(Calendar.SECOND, 59);
        dateEnd.set(Calendar.MILLISECOND,999);
        return dateEnd.getTime();
    }


    /**
     * 获取当天开始时间
     * @param date
     * @return
     */
    public static Date getStartTime(Date date) {
        Calendar dateStart = Calendar.getInstance();
        dateStart.setTime(date);
        dateStart.set(Calendar.HOUR_OF_DAY, 0);
        dateStart.set(Calendar.MINUTE, 0);
        dateStart.set(Calendar.SECOND, 0);
        dateStart.set(Calendar.MILLISECOND,0);
        return dateStart.getTime();
    }

    /**
     * 修复毫秒数据
     */
    public static Date fixMilliSecond(Date date,boolean end){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if(end){
            calendar.set(Calendar.MILLISECOND,999);
        }else{
            calendar.set(Calendar.MILLISECOND,0);
        }
        return calendar.getTime();
    }

    /**
     * 获取当天结束时间
     * @param date
     * @return
     */
    public static String getEndTimeStr(Date date) {
        Calendar dateEnd = Calendar.getInstance();
        dateEnd.setTime(date);
        dateEnd.set(Calendar.HOUR_OF_DAY, 23);
        dateEnd.set(Calendar.MINUTE, 59);
        dateEnd.set(Calendar.SECOND, 59);
        return format(dateEnd.getTime(),DATE_TIME_PATTERN);
    }


    /**
     * 获取当天开始时间
     * @param date
     * @return
     */
    public static String getStartTimeStr(Date date) {
        Calendar dateStart = Calendar.getInstance();
        dateStart.setTime(date);
        dateStart.set(Calendar.HOUR_OF_DAY, 0);
        dateStart.set(Calendar.MINUTE, 0);
        dateStart.set(Calendar.SECOND, 0);
        dateStart.set(Calendar.MILLISECOND,0);
        return format(dateStart.getTime(),DATE_TIME_PATTERN);
    }

    /**
     * 获取秒
     * @param date 格式 必须为 00:00
     * @return
     */
    public static int getMin(String date){
       String[] s =  date.split(":");
       int re =  Integer.parseInt(s[0])*60+Integer.parseInt(s[1]);
       return re;
    }

    /**
     * 获取上个月结算月份 字符串
     * @param date
     * @return
     */
    public static String getLastSetlMon(Date date){
        date = add(date,Calendar.MONTH,-1);
        return format(date,"yyyyMM");
    }


        public static void main(String[] args) {
            System.out.println(getSunday(new Date())
        );
    }
}
